﻿// Copyright richyp 2009
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Richyp.Bodger {
    public class BaseUnit {
        public int ID { get; set; }
        public string Name { get; set; }
        private List<Attribute> attributes = new List<Attribute>();
        public List<Attribute> Attributes {
            get {
                return this.attributes;
            }
        }

        public static BaseUnit CreateFromNode(XmlNode node) {
            BaseUnit unit = new BaseUnit();
            foreach (XmlNode child in node.ChildNodes) {
                string name = child.Name.ToUpper();
                string value = child.InnerText;
                if (name == "ID") {
                    unit.ID = int.Parse(value);
                } else if (name == "NAME") {
                    unit.Name = value;
                } else if (name == "ATTRIBUTES") {
                    foreach (XmlNode attrNode in child.ChildNodes) {
                        Attribute attribute = new Attribute();
                        attribute.Name = attrNode.Name;
                        attribute.Value = int.Parse(attrNode.InnerText);
                        unit.attributes.Add(attribute);
                    }
                }
            }
            return unit;
        }

        public Attribute GetAttribute(string name) {
            foreach (Attribute attr in this.attributes) {
                if (attr.Name.ToUpper() == name.ToUpper()) {
                    return attr;
                }
            }
            return null;
        }

        public int GetAttributeValue(string name) {
            Attribute attr = this.GetAttribute(name);
            if (attr == null) {
                return -1;
            }
            return attr.Value;
        }
    }
}
